# -*- coding: utf-8 -*-
import logging
from DB import DbSearchQueries


# We are using django's simplejson
# module to format JSON strings:
from django.utils import simplejson


def getAllCoursesOfSem(currSemester,currUniversity):
    coursesList = DbSearchQueries.create_query_of_get_all_courses_by_sem(currSemester,currUniversity)
    jsonCoursesList = simplejson.dumps({
                                        "Courses":   coursesList
        })
    return jsonCoursesList

def getFacultiesAndDeparts(currUniversity,currSemester):
        
        faculties = DbSearchQueries.create_query_of_faculties(currUniversity,currSemester)
        jsonFacultiesList = simplejson.dumps({
                "faculties"  : faculties
        })
        return jsonFacultiesList
                              
def getCoursesListByDepart(currUniversity,currSemester,departNum):    
        
        coursesList = []
        
        courses_result_query = DbSearchQueries.create_query_of_courses_in_hug(currUniversity,currSemester,departNum)
        
        for course in courses_result_query:
                coursesList.append({'cName': course['cName'], 'cNum': course['cNum']})
        
        jsonCoursesList = simplejson.dumps({
                "courses"  : coursesList
        })
         
        return jsonCoursesList
    
def getCoursesListByDepartAndDay(currUniversity,currSemester,deptNum,dayStr):
        if dayStr == "1":
            dayStr = u"א"
        if dayStr == "2":
            dayStr = u"ב"
        if dayStr == "3":
            dayStr = u"ג"
        if dayStr == "4":
            dayStr = u"ד"
        if dayStr == "5":
            dayStr = u"ה"
        if dayStr == "6":
            dayStr = u"ו"
        coursesList = []
        courses_result_query = DbSearchQueries.create_query_of_courses_in_hug_by_day(currUniversity,currSemester,deptNum,dayStr)
        for course in courses_result_query:
                coursesList.append({'cName': course['cName'], 'cNum': course['cNum']})
        jsonCoursesList = simplejson.dumps({
                "courses"  : coursesList
        })
             
        return jsonCoursesList
 


# The groups (01,02,03) will be ordered in increasing order
def getChosenCoursesInfo(currUniversity, currSemester,chosenCoursesList):

        coursesList = []
        for curr_course in chosenCoursesList:                
            #get course from DB
            coursesList.append(DbSearchQueries.create_query_of_course_info(currUniversity, currSemester,curr_course[:4],curr_course))
                
        jsonCoursesInfo = simplejson.dumps({
                "coursesInfo"  : coursesList
        })
        return jsonCoursesInfo
                 
def getCourseProfileInformationFromModels(currUniversity, currSemester,department, courseNum):
        #get course from DB

        course_result_query = DbSearchQueries.create_query_of_course_info(currUniversity, currSemester,department,courseNum)
        jsonCourseInfo = simplejson.dumps({
                "courseInfo"  : [course_result_query]
        })
        return jsonCourseInfo
    


def getCourseBidding(currUniversity,department, courseNum):
    course_result_query = DbSearchQueries.create_query_of_course_bidding(currUniversity,department,courseNum)
    jsonCourseBid = simplejson.dumps(course_result_query)
    return jsonCourseBid

def getCourseSyllabus(currUniversity,courseNum):
    return DbSearchQueries.create_query_of_course_syllabous(currUniversity, courseNum)

def saveUserData(University, userId, userEmail, nickName,info,semesterToSave,chosenCoursesLists):
    return DbSearchQueries.save_data(University, userId, userEmail, nickName,info,semesterToSave,chosenCoursesLists)
   
def loadUserData(University, userId, userEmail, nickName):
    info = DbSearchQueries.load_data(University, userId, userEmail, nickName)
    jsonUserInfo = simplejson.dumps(info)
    return jsonUserInfo

def getFriendList(currUniversity,currSemester,departNum,courseNum,friends):
    friends_list = DbSearchQueries.create_query_of_friend_list_for_course(currUniversity, currSemester, departNum, courseNum, friends)
    total_selected = friends_list[1]
    jsonFriendsInfo = simplejson.dumps({'friendsList': friends_list[0], 'totalCourseSelected': total_selected})
    return jsonFriendsInfo

def getFriendChosenCourses(currUniversity,userFacebookId):
    friend_courses = DbSearchQueries.getFriendCourses(currUniversity,userFacebookId)
    jsonFriendCourses = simplejson.dumps({'friendCourses': friend_courses})
    return jsonFriendCourses
    

def saveFBGoogle(currUniversity,fbID,googleID):
    return DbSearchQueries.save_fb_and_google_ids(currUniversity,fbID,googleID)

#get the data structure of event links of the user
def getEventsToDelete(currUniversity,userId):
    return  DbSearchQueries.load_events(currUniversity,userId)

#save new event links data structure of the user
def saveEventsSynced(currUniversity,userId,eventLinkList,userEmail,userNickname):
    return DbSearchQueries.save_events(currUniversity,userId,eventLinkList,userEmail,userNickname)
    